﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;
using System.Data.Linq;

namespace SharingCenter.Web.Services
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using System.Web.Script.Services;
    using System.Xml.XPath;
    using System.Text;
    using SharingCenter.Web.Classes.Account;
    using SharingCenter.Web.Classes.Profiles;
    /// <summary>
    /// Summary description for MessageServices
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [ScriptService]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class MessageServices : System.Web.Services.WebService
    {
        //[WebMethod]
        //public void SendMessage(EntityType senderEntity, Guid from, Guid to, string message, EntityType contextType, Guid contextId1, Guid contextId2)
        //{
        //    if (senderEntity == EntityType.Default)
        //    {
        //        from = WebConsts.FAKE_GUID;
        //    }

        //    Sc_Message msg = new Sc_Message();
        //    msg.MessageId = Guid.NewGuid();
        //    msg.IsMain = true;
        //    msg.ContextType = contextType;
        //    msg.ContextId = contextId1;
        //    msg.ContextId2 = contextId2;
        //    msg.MessageContent = message;

        //    List<object> guidList = new List<object>();
        //    guidList.Add(to);
        //    string xmlTo = Utility.SerializeSimpleList(guidList);
        //}

        //[WebMethod]
        //public int GetUnreadMessage(string guidUserId)
        //{
        //    int unread = 0;
        //    try
        //    {
        //        Guid uid = new Guid(guidUserId);
        //        var q = from t in scDataContext.Instance.Sc_Messages
        //                where t.RecipientList.Contains(guidUserId) && (t.ReadRecipientList == null || !t.ReadRecipientList.Contains(guidUserId))
        //                select t.MessageId;
        //        unread = q.Count();

        //    }
        //    catch {
        //        unread = -1;
        //    }
        //    return unread;
        //}

        //[WebMethod]
        //public int GetUnreadRequest(string guidUserId)
        //{
        //    int unread = 0;
        //    try
        //    {
        //        Guid uid = new Guid(guidUserId);
        //        var q = from t in scDataContext.Instance.Sc_SharedRequests
        //                where t.RecipientUserId == uid && t.Status == RequestStatus.Pending
        //                select t.RequestId;
        //        unread = q.Count();

        //    }
        //    catch
        //    {
        //        unread = -1;
        //    }
        //    return unread;
        //}

        [WebMethod]
        [ScriptMethod]
        public string GetAllUnreadItems(string userId)
        {
            Guid uid;
            try
            {
                uid = new Guid(userId);
            }
            catch{return string.Empty;}
            //Dictionary<string, int> ret = new Dictionary<string, int>();
            WebProfile p = AccountOperations.GetWebProfile(uid);
            StringBuilder ret = new StringBuilder();
            GetAllUnhandledItemsResult rst = (GetAllUnhandledItemsResult)scDataContext.Instance.GetAllMsgForUser(uid, p.Website.LastMessageRetrievalDate).First();
            ret.AppendFormat("'Message':{0},'Request':{1}",
                rst.unreadMessages.HasValue ? (int)rst.unreadMessages : 0,
                rst.pendingRequests.HasValue ? (int)rst.pendingRequests : 0
                );

            return "{" + ret.ToString() + "}";
        }
    }

    //    [WebMethod]
    //    public List<Sc_Message> GetMessageBySenderEntity(EntityType entity, string guidUserId)
    //    {
    //        List<Sc_Message> ret = null;
    //        try
    //        {
    //            Guid uid = new Guid(guidUserId);
    //            var q = from t in scDataContext.Instance.Sc_Messages
    //                    where t.RecipientList.Contains(guidUserId) && t.SenderEntityType == entity
    //                    orderby t.MessageDate descending
    //                    select t;
    //            ret = q.ToList();
    //        }
    //        catch {  }
    //        return ret;
    //    }
}
